package com.example.animations;

import android.os.Bundle;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends Activity {
	private CheckBox mCheck1;
	private CheckBox mCheck2;
	private CheckBox mCheck3;
	private CheckBox mCheck4;
	private Button mMoreButton;
	private boolean mExtrasShown = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		getActionBar().setTitle("Animation Example");
		
		//Find the important Views
		mCheck1 = (CheckBox) findViewById(R.id.choice1);
		mCheck2 = (CheckBox) findViewById(R.id.choice2);
		mCheck3 = (CheckBox) findViewById(R.id.extra_choice3);
		mCheck4 = (CheckBox) findViewById(R.id.extra_choice4);
		mMoreButton = (Button) findViewById(R.id.more_choices);
		
		mMoreButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//Check if the extra options are
				//already shown
				if(mExtrasShown){
					//The extra options should be hidden
					slideOutOptions();
				}else{
					//The extra options should be shown
					slideInOptions();
				}
				
				//Change the state of whether or not the
				//extra checkboxes are shown
				mExtrasShown = !mExtrasShown;
			}
		});
		
		//Restore state from 
		if(savedInstanceState != null){
			restoreCheckboxes(savedInstanceState);
		}
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putBoolean("check1", mCheck1.isChecked());
		outState.putBoolean("check2", mCheck2.isChecked());
		outState.putBoolean("check3", mCheck3.isChecked());
		outState.putBoolean("check4", mCheck4.isChecked());
		outState.putBoolean("extras_shown", mExtrasShown);
	}

	/**
	 * Restores the checkbox state over pauses and stops
	 * @param savedInstanceState Bundle of saved instance state
	 */
	private void restoreCheckboxes(Bundle savedInstanceState){
		boolean check1 = false;
		boolean check2 = false;
		boolean check3 = false;
		boolean check4 = false;
		
		if(savedInstanceState.containsKey("check1")){
			check1 = savedInstanceState.getBoolean("check1");
		}
		if(savedInstanceState.containsKey("check2")){
			check2 = savedInstanceState.getBoolean("check2");
		}
		if(savedInstanceState.containsKey("check3")){
			check3 = savedInstanceState.getBoolean("check3");
		}
		if(savedInstanceState.containsKey("check4")){
			check4 = savedInstanceState.getBoolean("check4");
		}
		
		mCheck1.setChecked(check1);
		mCheck2.setChecked(check2);
		mCheck3.setChecked(check3);
		mCheck4.setChecked(check4);
		
		if(savedInstanceState.containsKey("extras_shown")){
			if(savedInstanceState.getBoolean("extras_shown")){
				mExtrasShown = true;
				slideInOptions();
			}
		}
	}
	
	
	/**
	 * Show the extra options
	 */
	private void slideInOptions(){
		mMoreButton.setText("Hide Extra Choices");
		
		//Translate the checkboxes 100 pixels
		//left and make them completely transparent
		mCheck3.setX(-100);
		mCheck4.setX(-100);
		
		//Animate to being opaque and
		//slide in checkboxes
		//delay the second checkbox animation
		//75 milliseconds for extra effect
		mCheck3.animate()
				.alpha(1f)
				.translationX(0)
				.setDuration(250)
				.start();
		mCheck4.animate()
				.alpha(1f)
				.translationX(0)
				.setDuration(250)
				.setStartDelay(75)
				.start();
	}
	
	/**
	 * Hide the extra options
	 */
	public void slideOutOptions(){
		mMoreButton.setText("Show Extra Choices");
		
		//Slide out animation
		mCheck3.animate()
				.alpha(0f)
				.translationX(-100)
				.setDuration(250)
				.start();
		mCheck4.animate()
				.alpha(0f)
				.translationX(-100)
				.setDuration(250)
				.setStartDelay(75)
				.start();
	}
	
}
